﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace CommonTools.UserControls.Toast
{
    public enum ToastEnum
    {
        完成,
        信息,
        警告,
        错误
    }


    /// <summary>
    /// Toast.xaml 的交互逻辑
    /// </summary>
    public partial class Toast : UserControl
    {


        public ToastEnum Type
        {
            get { return (ToastEnum)GetValue(TypeProperty); }
            set { SetValue(TypeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Type.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TypeProperty =
            DependencyProperty.Register("Type", typeof(ToastEnum), typeof(Toast), new PropertyMetadata(ToastEnum.完成, (sender, e) =>
            {
                var dp = sender as Toast;
                if (dp == null) return;
                dp.TypeChanged();
            }));



        public string IconPath
        {
            get { return (string)GetValue(IconPathProperty); }
            set { SetValue(IconPathProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IconPath.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IconPathProperty =
            DependencyProperty.Register("IconPath", typeof(string), typeof(Toast), new PropertyMetadata($"{System.AppDomain.CurrentDomain.BaseDirectory}\\UserControls\\Toast\\完成.png)"));




        public string Message
        {
            get { return (string)GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Message.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register("Message", typeof(string), typeof(Toast), new PropertyMetadata(string.Empty));




        public int ShowMs
        {
            get { return (int)GetValue(ShowMsProperty); }
            set { SetValue(ShowMsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ShowMs.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ShowMsProperty =
            DependencyProperty.Register("ShowMs", typeof(int), typeof(Toast), new PropertyMetadata(5000));

        Storyboard story = new Storyboard();
        bool isClosing = false;

        private void TypeChanged()
        {
            IconPath = $"{System.AppDomain.CurrentDomain.BaseDirectory}\\UserControls\\Toast\\{Type}.png)";
        }

        public Toast(string message, ToastEnum type = ToastEnum.完成, int showMs = 5000)
        {
            Message = message;
            Type = type;
            ShowMs = showMs;
            this.VerticalAlignment = VerticalAlignment.Top;
            this.HorizontalAlignment = HorizontalAlignment.Center;
            this.Margin = new Thickness(0, 5, 0, 0);
            DispatcherTimer timer = new DispatcherTimer() { Interval = new TimeSpan(0,0,0,0,showMs) };
            timer.Tick += Timer_Tick;
            timer.Start();
            InitializeComponent();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            DispatcherTimer timer = sender as DispatcherTimer;
            if (timer == null) return;
            timer.Stop();
            BeginClosed();
            //Dispatcher.Invoke(new Action(() =>
            //{

            //}));
        }

        private void BeginClosed()
        {
            if (isClosing) return;
            isClosing = true;
            DoubleAnimation animation = new DoubleAnimation()
            {
                To = 0,
                From = 1,
                Duration = new Duration(new TimeSpan(0, 0, 1)),
                FillBehavior = FillBehavior.Stop,
                AutoReverse = false
                
            };
            Storyboard.SetTarget(animation, this);
            Storyboard.SetTargetProperty(animation, new PropertyPath(UserControl.OpacityProperty));
            story.Children.Add(animation);
            story.Completed += Story_Completed;
            story.Begin();
        }

        private void Story_Completed(object sender, EventArgs e)
        {
            Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                ToastHelper.Toast.ToastList.Remove(this);
            }));
        }

        private void self_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            BeginClosed();
        }

        public static void Show(string message, ToastEnum type = ToastEnum.完成, int showMs = 5000)
        {
            Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                Toast toast = new Toast(message, type, showMs);
                ToastHelper.Toast.ToastList.Insert(0, toast);
            }));
        }
    }
}
